package com.peas.platform.module.icims.budget.project.domain.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.budget.apply.dto.BudgetOneDto;
import com.peas.platform.module.icims.budget.apply.service.BudgetOneService;
import com.peas.platform.module.icims.budget.project.adapter.repository.BudgetProjectTwoRepository;
import com.peas.platform.module.icims.budget.project.adapter.repository.po.BudgetProjectTwoPo;
import com.peas.platform.module.icims.budget.project.domain.BudgetProjectTwoDomain;
import com.peas.platform.module.icims.budget.project.dto.*;
import com.peas.platform.module.icims.budget.project.service.BudgetProjectTwoFileService;
import com.peas.platform.module.icims.budget.project.service.BudgetProjectTwoItemService;
import com.peas.platform.module.icims.budget.project.service.BudgetProjectTwoKpiService;
import com.peas.platform.module.icims.budget.project.service.BudgetProjectTwoYearService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class BudgetProjectTwoDomainImpl extends BaseDomainImpl<BudgetProjectTwoDto, BudgetProjectTwoPo> implements BudgetProjectTwoDomain {

    @Resource
    private BudgetProjectTwoRepository budgetProjectTwoRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private BudgetOneService budgetOneService;
    @Resource
    private BudgetProjectTwoItemService budgetProjectTwoItemService;
    @Resource
    private BudgetProjectTwoYearService budgetProjectTwoYearService;
    @Resource
    private BudgetProjectTwoKpiService budgetProjectTwoKpiService;
    @Resource
    private BudgetProjectTwoFileService budgetProjectTwoFileService;

    @Override
    public BaseRepository<BudgetProjectTwoPo> getMapper() {
        return budgetProjectTwoRepository;
    }

    @Override
    public Class<? extends BaseRepository<BudgetProjectTwoPo>> getRepositoryClass() {
        return BudgetProjectTwoRepository.class;
    }

    @Transactional
    @Override
    public int save(BudgetProjectTwoDto dto) {
        if (StrUtil.isBlank(dto.getCode())) {
            String code = sequenceService.genSequence("budget", "BudgetProjectTwoDto", "BPT", 4, 1);
            dto.setCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BudgetProjectTwoDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("code", dto.getCode());
        return result;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        List<BudgetOneDto> list = budgetOneService.listByField("projectTwoId", id);
        Assert.isTrue(CollUtil.isEmpty(list), "有关联的一上预算编制存在");
        // items
        List<BudgetProjectTwoItemDto> ptids = budgetProjectTwoItemService.listByField("projectTwoId", id);
        budgetProjectTwoItemService.removeBatchByList(ptids);
        // years
        List<BudgetProjectTwoYearDto> ptyds = budgetProjectTwoYearService.listByField("projectTwoId", id);
        budgetProjectTwoYearService.removeBatchByList(ptyds);
        // kpis
        List<BudgetProjectTwoKpiDto> ptkds = budgetProjectTwoKpiService.listByField("projectTwoId", id);
        budgetProjectTwoKpiService.removeBatchByList(ptkds);
        // files
        List<BudgetProjectTwoFileDto> ptfds = budgetProjectTwoFileService.listByField("projectTwoId", id);
        budgetProjectTwoFileService.removeBatchByList(ptfds);
        return super.removeById(id);
    }

}
